package com.woniuxy.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.woniuxy.controller.form.*;
import com.woniuxy.controller.info.FindConfInfo;
import com.woniuxy.controller.info.GetConfByCatCodeInfo;
import com.woniuxy.controller.info.GetOneConfInfo;
import com.woniuxy.dao.PawnAttrGroupCatRelDao;
import com.woniuxy.dao.PawnAttrGroupDao;
import com.woniuxy.dao.PawnGoodsAttrRelDao;
import com.woniuxy.exception.AttrConfNameExsitsException;
import com.woniuxy.exception.CheckedIsNullException;
import com.woniuxy.exception.DdException;
import com.woniuxy.exception.ProductCatNameExsitsException;
import com.woniuxy.model.PawnAttrConf;
import com.woniuxy.dao.PawnAttrConfDao;
import com.woniuxy.model.PawnAttrGroup;
import com.woniuxy.model.PawnAttrGroupCatRel;
import com.woniuxy.model.PawnGoodsAttrRel;
import com.woniuxy.service.PawnAttrConfService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.woniuxy.utils.NewCopyBean;
import com.woniuxy.utils.Result;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author hujianying
 * @since 2022-03-23
 */
@Service
public class PawnAttrConfServiceImpl extends ServiceImpl<PawnAttrConfDao, PawnAttrConf> implements PawnAttrConfService {


    /**
     * 属性组表
     */
    @Resource
    private PawnAttrGroupDao pagd;

    @Resource
    private PawnGoodsAttrRelDao pgard;
    /**
     * 根据属性组id查询所有对应的属性配置
     * @param fcf groupCode
     * @return findConfInfo
     */
    @Override
    public Result getAllConfByGroupCode(FindConfForm fcf) {

        Page<PawnAttrConf> findConfForm = baseMapper.selectPage(fcf, new QueryWrapper<PawnAttrConf>().eq("group_code", fcf.getGroupCode()));
        Page<FindConfInfo> findConfInfoPage = new Page<>();
        List<FindConfInfo> findConfInfos = NewCopyBean.copyList(findConfForm.getRecords(), FindConfInfo.class);

        for(FindConfInfo f : findConfInfos){
            f.setPutType("列表选择");
            String options = f.getOptions();
            String replace = options.replace("[", "");
            String replace1 = replace.replace("]", "");
            String replace2 = replace1.replace("\"", "");
            f.setOptions(replace2);
        }
        findConfInfoPage.setRecords(findConfInfos);
        findConfInfoPage.setCurrent(findConfForm.getCurrent());
        findConfInfoPage.setSize(findConfForm.getSize());
        findConfInfoPage.setTotal(findConfForm.getTotal());
        return Result.success(200,"查询成功",findConfInfoPage);
    }

    /**
     * 模糊查询
     * @param fcf str
     * @return findConfInfo
     */
    @Override
    public Result getConfByLike(FindConfForm fcf) {
        QueryWrapper<PawnAttrConf> wrapper = new QueryWrapper<>();
        wrapper.eq("group_code", fcf.getGroupCode());
        wrapper.like("attr_name",fcf.getStr());
        Page<PawnAttrConf> findConfForm = baseMapper.selectPage(fcf, wrapper);
        List<FindConfInfo> findConfInfos = NewCopyBean.copyList(findConfForm.getRecords(), FindConfInfo.class);
        for(FindConfInfo f : findConfInfos){
            f.setPutType("列表选择");
            String options = f.getOptions();
            String replace = options.replace("[", "");
            String replace1 = replace.replace("]", "");
            String replace2 = replace1.replace("\"", "");
            f.setOptions(replace2);
        }
        Page<FindConfInfo> findConfInfoPage = new Page<>();
        for(FindConfInfo f : findConfInfos){
            f.setPutType("列表选择");
        }
        findConfInfoPage.setRecords(findConfInfos);
        findConfInfoPage.setTotal(findConfForm.getTotal());
        findConfInfoPage.setSize(findConfForm.getSize());
        findConfInfoPage.setCurrent(findConfForm.getCurrent());
        return Result.success(200,"查询成功",findConfInfoPage);
    }

    /**
     * 根据被选中的属性删除
     * @param dcf isChecked
     * @return data为null
     */
    @Override
    @Transactional
    public Result deleteByChecked(DeleteConfForm dcf) throws DdException {
        List<FindConfInfo> isChecked = dcf.getIsChecked();
        if(ObjectUtils.isEmpty(isChecked)){
            throw new CheckedIsNullException(50009,"请先勾选删除");
        }else{
            for(FindConfInfo f : isChecked){
                List<PawnGoodsAttrRel> attr_code = pgard.selectList(new QueryWrapper<PawnGoodsAttrRel>().eq("attr_code", f.getAttrCode()));
                if(!ObjectUtils.isEmpty(attr_code)){
                    throw new ProductCatNameExsitsException(50002,"该属性参数下已有商品存在");
                }
            }
            for(FindConfInfo f : isChecked){
                baseMapper.deleteById(f.getAttrCode());
                pgard.delete(new QueryWrapper<PawnGoodsAttrRel>().eq("attr_code",f.getAttrCode()));
            }
            return Result.success(200,"删除成功",null);
        }
    }

    /**
     * 根据attrCode获取conf
     * @param gocf attrCode
     * @return
     */
    @Override
    public Result getConfByAttrCode(GetOneConfForm gocf) {
        PawnAttrConf conf = baseMapper.selectOne(new QueryWrapper<PawnAttrConf>().eq("attr_code", gocf.getAttrCode()));
        String options = conf.getOptions();
        String replace = options.replace("[", "");
        String replace1 = replace.replace("]", "");
        String replace2 = replace1.replace("\"", "");
        GetOneConfInfo getOneConfInfo = NewCopyBean.copyOne(conf, GetOneConfInfo.class);
        getOneConfInfo.setOptions(replace2);
        return Result.success(200,"请求成功",getOneConfInfo);
    }

    /**
     * 修改属性参数
     * @param ucf UpdateConfForm
     * @return data为null
     */
    @Override
    @Transactional
    public Result updateConf(UpdateConfForm ucf) {
        PawnAttrConf pawnAttrConf = baseMapper.selectById(ucf.getAttrCode());
        PawnAttrConf pawnAttrConf1 = NewCopyBean.copyOne(ucf, PawnAttrConf.class);
        String options = ucf.getOptions();
        String s = "";
        String[] split = options.split(",|，");
        for(int i = 0 ;i<split.length;i++){
            if(i==split.length-1){
                split[i] = "\""+split[i]+"\"";
                s+=split[i];
                break;
            }
            split[i] = "\""+split[i]+"\",";
            s+=split[i];
        }
        pawnAttrConf1.setGroupCode(pawnAttrConf.getGroupCode());
        pawnAttrConf1.setOptions("["+s+"]");
        baseMapper.updateById(pawnAttrConf1);
        //删除参数表和商品关联表
        pgard.delete(new QueryWrapper<PawnGoodsAttrRel>().eq("attr_code",ucf.getAttrCode()));
        return Result.success(200,"修改成功",null);
    }

    /**
     * 新增属性参数
     * @param scf saveConfForm
     * @return data为null
     */
    @Override
    @Transactional
    public Result saveConf(SaveConfForm scf) throws DdException{
        QueryWrapper<PawnAttrConf> wrapper = new QueryWrapper<PawnAttrConf>().eq("attr_name", scf.getAttrName());
        wrapper.eq("group_code",scf.getGroupCode());
        PawnAttrConf conf = baseMapper.selectOne(wrapper);
        if(ObjectUtils.isEmpty(conf)){
            PawnAttrConf pawnAttrConf = NewCopyBean.copyOne(scf, PawnAttrConf.class);
            String options = scf.getOptions();
            String s = "";
            String[] split = options.split(",|，");
            for(int i = 0 ;i<split.length;i++){
                if(i==split.length-1){
                    split[i] = "\""+split[i]+"\"";
                    s+=split[i];
                    break;
                }
                split[i] = "\""+split[i]+"\",";
                s+=split[i];
            }
            pawnAttrConf.setOptions("["+s+"]");
            baseMapper.insert(pawnAttrConf);
            return Result.success(200,"新增成功",null);
        }else{
            throw new AttrConfNameExsitsException(50010,"该属性已存在");
        }
    }

    /**
     * 根据大类code获取属性组参数
     * @param gcbccf catCode
     * @return GetConfByCatCodeInfo
     */
    @Override
    public Result getConfByPcatCode(GetConfByCatCodeForm gcbccf) {
        System.out.println(gcbccf);
        QueryWrapper<PawnAttrGroup> wrapper = new QueryWrapper<>();
        wrapper.eq("group_name",gcbccf.getGroupName());
        PawnAttrGroup pawnAttrGroup = pagd.selectOne(wrapper);
        List<PawnAttrConf> confs = baseMapper.selectList(new QueryWrapper<PawnAttrConf>().eq("group_code", pawnAttrGroup.getGroupCode()));
        List<GetConfByCatCodeInfo> getConfByCatCodeInfos = NewCopyBean.copyList(confs, GetConfByCatCodeInfo.class);
        return Result.success(200,"请求成功",getConfByCatCodeInfos);
    }
}
